<template>
  <div class="New_build">
    <div class="mask"></div>
    <div class="new_build2">
        
      <div class="build-top">
        <p>改单（仓库调拨查询）</p>
        <i class="el-icon-close" @click="closeChange"></i>
      </div>

      <div class="build-info">
        <div class="b-btn">
          <el-button @click="query" type="primary" icon="el-icon-search">查询</el-button>
          <!-- <el-button @click="odChange" icon="el-icon-edit-outline">修改</el-button> -->
        </div>
        <div class="build-info-1">
          <p>调拨日期</p>
          <el-date-picker
            class="build-inp"
            value-format="yyyy-MM-dd"
            v-model="allotTime"
            @change="leave"
            type="date">
          </el-date-picker>

        <p class="t_content">调出仓库</p>
        <el-input disabled style="width:60px;margin-right:10px;" type="text" v-model="calloutWarehouse" @blur="calloutWarehouseBlur"></el-input>
        <el-select disabled style="width:100px;" v-model="calloutWarehouseName" @change="queryNo">
          <el-option
            v-for="item in warehouseList"
            :key="item.value + Math.random()"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>

          <p>数量</p>
          <el-input class="build-inp" v-model="number" @blur="leave1" :disabled="Dis"></el-input>
          
          <p>款号</p>
          <el-input class="build-inp" v-model="kindsNumber"></el-input>
        </div>
        <div class="build-info2"> 
            <p>调拨序号</p>
            <el-input disabled class="build-inp" v-model="allotSequence"></el-input>

            <p class="t_content">调入仓库</p>
            
            <el-input disabled style="width:60px;margin-right:10px;" type="text" v-model="foldWarehouse" @blur="foldWarehouseBlur"></el-input>
            <div class="jl">
            <el-select disabled v-model="foldWarehouseName" @change="queryNo1" style="width:100px;">
              <el-option
                v-for="item in manufacturerList"
                :key="item.value + Math.random()"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            </div>

            <p>售价</p>
            <el-input disabled class="build-inp" v-model="sellingPrice"></el-input>
            <p>调出仓名</p>
            <el-input disabled class="build-inp" v-model="calloutWarehouseName"></el-input>
        </div>

        <div class="build-info2">
            <p>备注</p>
            <el-input style="width:425px;" @input="leave" v-model="remark"></el-input>
        </div>

        <el-table
          height="280px"
          :data="barcodeData"
          highlight-current-row
          @row-click="rowClick"
          style="width: 100%"
          :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}"
          :cell-style="{padding:0,height:'36px'}">
          <el-table-column
            prop="allotSequence"
            label="调拨序号"
            width="120px">
          </el-table-column>
          <el-table-column
            prop="foldWarehouse"
            label="调入仓库">
          </el-table-column>
          <el-table-column
            prop="calloutWarehouse"
            label="调出仓库">
          </el-table-column>
          <el-table-column
            prop="calloutWarehouseName"
            label="调出仓名">
          </el-table-column>
          <el-table-column
            prop="kindsNumber"
            label="款号"
            show-overflow-tooltip
            width="105px">
          </el-table-column>
          <el-table-column
            prop="number"
            label="数量">
          </el-table-column>
          <el-table-column
            prop="allotTime"
            label="调拨日期"
            width="100px">
            </el-table-column>
            <el-table-column
            prop="remark"
            label="备注">
            </el-table-column>
            <el-table-column
            prop="operator"
            label="操作员">
            </el-table-column>
            <el-table-column
            prop="enteringTime"
            label="录入日期"
            width="100px">
            </el-table-column>
            <el-table-column
            prop="status"
            label="修改状态">
            </el-table-column>
        </el-table>
      </div>
      
      <div class="build-btn" >
        <div style="margin:5px 20px 0 0">
          <el-button @click="closeChange">取消</el-button>
          <el-button type="primary" plain @click="saveChange">保存</el-button>
          <!-- <el-button type="primary" @click="keepGoing">继续录入</el-button> -->
        </div>
      </div>
      
    </div>

  </div>
</template>


<script>
import today from '../../../../components/mixins/today'
// import { kindsNumberOutContinue, kindsNumberOut } from '../../../../network/customerOutTotal'
import { getWarehouse } from '../../../../network/warehouse'
import {isEnough} from '../../../../network/quarter'
export default {
  mixins:[today,today],
  created(){
    this.allotTime = this.getNowTime()//时间
    // 获取仓库信息
    console.log(45)
    getWarehouse().then(res => {
      console.log(res.data)
        let list = res.data.data
        list.sort((a,b) => {
          return a.no.localeCompare(b.no)
        })
        list.forEach(e => {
          this.manufacturerList.push({value:e.name,label:e.name,value1:e.no})
          this.warehouseList.push({value:e.name,label:e.name,value1:e.no})//调出 
          if (e.isMainWarehouse) {
            this.foldWarehouse = e.no
            this.foldWarehouseName = e.name
          }
        })
    })
    
  },
  props: {
    // warehouseData:{
    //   type:Array,
    //   default:[]
    // },
    showData: {
      type: Object,
      default: {}
    }
  },
  data(){
    return {
      details:[],
      barcodeData:[],
      Dis:true,
      showPopup:'none',
      allotTime:[],
      calloutWarehouse:'',
      calloutWarehouseName:'', 
      warehouseList:[],
      number:'',
      kindsNumber:'',
      allotSequence:'',
      foldWarehouse:'',
      foldWarehouseName:'',
      manufacturerList:[],
      sellingPrice:'',
      remark:'',
      numList:[],
      saveList:[],
      testList:[],
    }
  },
  methods: {
    clean(){
      this.allotTime = ''
      this.calloutWarehouse = ''
      this.calloutWarehouseName = ''
      this.number = ''
      this.kindsNumber = ''
      this.allotSequence = ''
      this.foldWarehouse = ''
      this.foldWarehouseName = ''
      this.sellingPrice = ''
      this.calloutWarehouseName = ''
      this.remark = ''
      this.barcodeData = []
    },

    odChange(){
      var io = []
      var ip = []
      var il = []
      var iu = {}
      for(let w of this.barcodeData){
        if(w.dataSource == 1){ 
          io.push(w.kindsNumber)
          ip.push(w.number)
          for(var f in io){
            for(var g in ip){
              if(f = g){
                iu[io[f]] = ip[g]
              }
            }
          }
          console.log(iu)
        }else if(w.dataSource == 0){
          il.push(w.kindsNumber)
        }
      }
      let params = {
        allotTime:this.allotTime,
        calloutWarehouse:this.calloutWarehouse,
        calloutWarehouseName:this.calloutWarehouseName,
        number:this.number,
        kindsNumber:this.kindsNumber,
        allotSequence:this.allotSequence,
        foldWarehouse:this.foldWarehouse,
        foldWarehouseName:this.foldWarehouseName,
        sellingPrice:this.sellingPrice,
        remark:this.remark,
        kindsNumberMap:iu,
        rfidList:il
      }
      this.$emit('odChange',params)
    },
    query(){
      for(let r of this.barcodeData){
        if(this.kindsNumber == r.kindsNumber){
          this.barcodeData = [r]
        }
      }
    },
    

    rowClick(e){
      localStorage.setItem('dbChangeList',JSON.stringify(this.testList))
      if(e.dataSource == 0){ 
          this.Dis = true
        }else if(e.dataSource == 1){
          this.Dis = false
        }
      this.number = e.number
      this.sellingPrice = e.sellingPrice
      this.kindsNumber = e.kindsNumber
      this.foldWarehouseName = e.foldWarehouseName
      this.foldWarehouse = e.foldWarehouse
      this.calloutWarehouse = e.calloutWarehouse
      this.calloutWarehouseName = e.calloutWarehouseName
      console.log(e)
      this.details = e
    },

    leave(){
      for(let s of this.barcodeData){
        if(this.kindsNumber == s.kindsNumber){
          s.remark = this.remark
        }
        s.allotTime = this.allotTime
      }

      for(let m of this.saveList){
        console.log(m.allotTime)
        if(this.kindsNumber == m.kindsNumber){
          m.remark = this.remark
        }
        m.allotTime = this.allotTime
      }
    },
    //修改数量
    leave1(){
      this.numList = JSON.parse(localStorage.getItem('dbChangeList'))
      // console.log(this.saveList)
      for(let a of this.numList){
        if(this.kindsNumber == a.kindsNumber){var number1 = Number(this.number) - Number(a.number)} //和原数据对比 获取改动数量的差值
      }

      for(let b of this.saveList){
        if(this.kindsNumber == b.kindsNumber){
          b.number = number1
          b.remark = this.remark
        }
        b.allotTime = this.allotTime
      }

      for(let s of this.barcodeData){
        if(this.kindsNumber == s.kindsNumber){
          s.number = Number(this.number)
          s.remark = this.remark
        }
        s.allotTime = this.allotTime
      }
    },

    //调出仓 编号
    queryNo(){
      for(let s of this.warehouseList){
        if(s.value == this.calloutWarehouseName){
          this.calloutWarehouse = s.value1
          if(this.calloutWarehouse == this.foldWarehouse){
            this.$message({
              type:'warning',
              offset:400,
              message:'调入仓库与调出仓库不能为同一个'
            });
            this.calloutWarehouse = ''
            this.calloutWarehouseName = ''
          }
        }
      }
      for(let g of this.barcodeData){
        g.calloutWarehouse = this.calloutWarehouse
        g.calloutWarehouseName = this.calloutWarehouseName
      }
    },
    queryNo1(){
      for(let s of this.manufacturerList){
        if(s.value == this.foldWarehouseName){
          this.foldWarehouse = s.value1
          if(this.foldWarehouse == this.calloutWarehouse){
            this.$message({
              type:'warning',
              offset:400,
              message:'调入仓库与调出仓库不能为同一个'
            });
            this.foldWarehouse = ''
            this.foldWarehouseName = ''
          }
        }
      }
      for(let g of this.barcodeData){
        g.foldWarehouse = this.foldWarehouse
        g.foldWarehouseName = this.foldWarehouseName
      }
    },

    // 调入仓库失去焦点
    foldWarehouseBlur(){
      if (this.foldWarehouse != '') {
        for (const i of this.manufacturerList) {
          if (this.foldWarehouse == i.value1) {
            this.foldWarehouseName = i.label
            return
          }
        }
        this.$message({
            message: '无此仓库编号',
            type: 'warning'
          })
        this.foldWarehouse = ''
        this.foldWarehouseName = ''
      } else {
        this.foldWarehouseName = ''
      }
    },
    //调出仓库编号失去焦点
    calloutWarehouseBlur(){
      if (this.calloutWarehouse != '') {
        for (const i of this.warehouseList) {
          if (this.calloutWarehouse == i.value1) {
            this.calloutWarehouseName = i.label
            return
          }
        }
        this.$message({
            message: '无此仓库编号',
            type: 'warning'
          })
        this.calloutWarehouse = ''
        this.calloutWarehouseName = ''
      } else {
        this.calloutWarehouseName = ''
      }
    },

    closeChange(){
      this.$emit('closeChange')
      this.clean()
    },
    saveChange(){

      // console.log(this.saveList)
      // console.log(this.barcodeData)
      var kindsNumberNumberMap = {}
      var No = ''
      var kindList = []
      for(let a of this.saveList){
        kindsNumberNumberMap[a.kindsNumber] = a.number //货号为 key值 数量 value
        No = a.calloutWarehouse
        kindList.push(a.kindsNumber)
      }
      let obj = {kindsNumberNumberMap,companyNo:No}
      console.log(obj)
      isEnough(JSON.stringify(obj)).then(res=>{ //库存是否足够 /扣库存
        console.log(res.data)
        if(res.data.code == 200 && res.data.data == null){
          this.$emit('saveChange',this.barcodeData)
          this.clean()
        }
        if(res.data.data != null){
          for(let b in this.barcodeData){
            for(let c of res.data.data){
              if(this.barcodeData[b].kindsNumber == c){
                this.barcodeData.splice(b,1)
              }
            }
          }

          if(this.barcodeData != ''){
            this.$emit('InsufficientList',res.data.data)
            this.$emit('saveChange',this.barcodeData)
            this.clean()
          }else{
            this.$message.error({message:'此序号下所有货号库存均不足',offset:400})
          }
        }
      })
      
      return
      console.log(11)
      var io = []
      var ip = []
      var il = []
      var iu = {}
      for(let w of this.barcodeData){
        if(w.dataSource == 1){ 
          io.push(w.kindsNumber)
          ip.push(w.number)
          for(var f in io){
            for(var g in ip){
              if(f = g){
                iu[io[f]] = ip[g]
              }
            }
          }
          console.log(iu)
        }else if(w.dataSource == 0){
          il.push(w.kindsNumber)
        }
      }
      let params = {
        allotTime:this.allotTime,
        calloutWarehouse:this.calloutWarehouse,
        calloutWarehouseName:this.calloutWarehouseName,
        number:this.number,
        kindsNumber:this.kindsNumber,
        allotSequence:this.allotSequence,
        foldWarehouse:this.foldWarehouse,
        foldWarehouseName:this.foldWarehouseName,
        sellingPrice:this.sellingPrice,
        remark:this.remark,
        kindsNumberMap:iu,
        rfidList:il
      }
      this.$emit('saveChange',params)
    },
  },
  watch:{
    kindsNumber(a,b){
      if(a){
        this.kindsNumber = a.toUpperCase().trim()
      }
    },
    showData(a,b){
      this.allotTime = a.allotTime.slice(0,10)
      this.allotSequence = a.allotSequence
      this.remark = a.remark
      for(let f of a.warehouseAllotInfoOneList){
        f.enteringTime = f.enteringTime.slice(0,10)
      }
      this.barcodeData = a.warehouseAllotInfoOneList
      this.testList = JSON.parse(JSON.stringify(this.barcodeData))
      this.saveList = JSON.parse(JSON.stringify(this.barcodeData))
      this.saveList.forEach(e=>{
        e.number = 0
      })
    },
    // warehouseData(a,b){
    //   console.log(a)
    //     a.forEach(e => {
    //     this.warehouseList.push({value:e.value,label:e.label})
    //     this.manufacturerList.push({value:e.value,label:e.label})
    // });
    // }
  },
  mounted(){

  },
}
</script>

<style scoped>
  .middle{
    padding: 10px 15px;
  }
  .middle p{
      padding-bottom: 10px;
  }
  ._top{
      display: flex;
      width: 100%;
      height: 40px;
      padding: 10px 15px;
      justify-content: space-between;
      background: #f8f8f8;
      font-weight: 700;
  }

  /* 新建资料 */
  .mask{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.3;
    z-index: 9;
  }
  .new_build2{
    position: fixed;
    /* position: relative; */
    top: 20%;
    left: 50%;
    transform: translateX(-50%);
    width: 1088px;
    height: 580px;
    z-index: 99;
    background-color: white;
    overflow: auto;
  }
  .new_build2 .build-top{
    height: 40px;
    background-color: #f8f8f8;
    display: flex;
    justify-content: space-between;
    padding: 10px 15px;
  }
  .build-top p{
      font-weight: 700;
  }


  .new_build2 input{
    height: 30px;
    border: buildlid 1px #DCDFE6;
    border-radius: 5px;
    margin-left: 10px;
  }
  .new_build2 input:focus{
    outline: none;
    border: buildlid 1px #409EFF;
  }
  .b-btn{
    margin: 10px 0;
    padding: 10px;
    background: #f4f4f4;
  }

  .build-info{
    padding: 0 15px;
    padding-bottom: 10px;
    /* border-bottom: buildlid #f2f2f2 2px; */
  }
  .build-info-1 p{
    display: inline-block;
    width: 80px;
    text-align: right;
    margin-right: 15px;
  }


  .build-info2{
    display:flex;
    margin:10px 0;
  }
  .build-info2 p{
    display:inline-block;
    width:80px;
    text-align:right;
    margin-right:15px;
  }

  .colorBox, .infoBox {
    display: inline;
  }


  .build-btn{
    text-align: right;
  }

  .build-inp{
      width: 160px;
  }

</style>

<style>
.New_build .new_build2 .build-info .build-info2 .jl .el-input__inner{
  width: 100px;
}
.new_build2 .build-info-4 .el-table .cell{
    box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
    word-break: break-all;
    line-height: 41px;
    padding-right: 10px;
    height: 40px;
}
.new_build2 .build-info-4 .el-input__inner{
  border: 0px;
}
.new_build2 .build-info-4 .el-input__inner:focus{
  border: 1px solid #017aff;
}

  .new_build2 .el-input__inner{
    height: 30px!important;
  }
  .new_build2 .el-input__icon{
    line-height: 30px!important;
  }

</style>